<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\model\activity\Apply;
use app\common\model\activity\Item;
use app\common\model\activity\News;
use app\common\model\Category;
use app\common\model\activity\Category as ActivityCategory;
use app\common\utils\Utils;

/**
 * 会员接口
 */
class Activity extends Api
{
    protected $noNeedLogin = ['activityCategory', 'activityNews', 'detail', 'activityIten', 'itemDetail'];
    protected $noNeedRight = '*';


    /**
     * 活动报道分类
     */
    public function activityCategory() {
        $list = ActivityCategory::all(function ($query) {
            $query->order('weigh desc')->where(['is_show'=> 1]);
        });

        $this->success('活动分类', $list);
    }

    /**
     * 分类下项目
     */
    public function activityNews() {
        $category_id = input('category_id/d', 0);
        if (!$category_id) {
            $this->error('分类参数错误');
        }

        $list = News::where(['is_show'=> 1, 'category_id'=> $category_id])
            ->order('createtime desc')
            ->field('content', true)
            ->paginate(15);

        $this->success('活动列表', $list);
    }

    /**
     * 活动报道详情
     */
    public function detail() {
        $news_id = input('news_id/d', 0);
        $info = News::get($news_id);

        $this->success('content', $info);
    }


    /**
     * 活动列表
     */
    public function activityIten() {
        $list = Item::where(['is_show'=> 1])
            ->order('createtime desc')
            ->field('content', true)
            ->paginate(15);

        $this->success('活动列表', $list);
    }

    public function search() {
        $search = input('keyword/s', '');
        $where = ['is_show'=> 1];
        if ($search) {
            $where['name'] = ['like', "%$search%"];
        }

        $list = News::where($where)
            ->field('content', true)
            ->order('createtime desc')->paginate(15);

        $this->success('视频列表', $list);

    }

    /**活动详情*/
    public function itemDetail() {
        $item_id = input('item_id/d', 0);
        $info = Item::get($item_id);

        $this->success('content', $info);
    }

    /**
     * 申请报名
     */
    public function apply() {
        $item_id = input('item_id/d', 0);
        $a = Apply::get([
            'activity_item_id'=> $item_id,
            'user_id' => UID
        ]);
        if ($a) {
            $this->error('无需重复报名');
        }

        Apply::create([
            'activity_item_id'=> $item_id,
            'user_id' => UID
        ]);

        $this->success('报名成功');
    }

    /**
     * 我的报名
     */

    public function getMyActive() {
        $where['user_id'] = UID;

        $list = Apply::where($where)->order('createtime desc')
            ->paginate()
            ->each(function ($item) {
                $item['createtime_text'] = Utils::time_format($item['createtime']);
                $item['activity'] = Item::get($item['activity_item_id'])->visible(['id', 'image_text', 'name', 'desc']);
            });


        $this->success('我的活动列表', $list);
    }

}
